<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/artdialog/skins/default.css" type="text/css" media="all" />
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>	
<script type="text/javascript">

$(document).ready(function(){

	function get_data()
	{
		var purchaseorders_id = $('#po_id').text();
		var purchaseorder_datails_id = $("#pd_id").text();
		var amount_paid_before = parseFloat($("#pd_pay_all").text());
		var refund_money_before = parseFloat($("#pd_payback_all").text());
		var refund_money = parseFloat($("#payback_refund_money").val().replace(/[ ]/g,""));//alert(refund_money);
		var remark = $("#payback_remark").val().replace(/[ ]/g,"");
		
		if(isNaN(refund_money) || refund_money==0)
		{
			alert("退款金额输入有误");
			return ;
		}
		
		if(refund_money > (amount_paid_before-refund_money_before) && remark == "")
		{
			alert("退款金额大于已付金额，需要填写备注");
			return ;
		}
		var data = {'purchaseorder_id':purchaseorders_id,'purchase_detail_id':purchaseorder_datails_id,'refund_money':refund_money,'remark':remark};

		return data;
	}

	$(".input_no_space").keyup(function(){
		$(this).val($(this).val().replace(/[ ]/g,""));
	});

	$("#add_payabck_btn").click(function(){
		//alert("ok");
		var data = get_data();
		var flag =false;
		if(data)
		{
			$.ajax({		
				type:'post',
				url:'<?php echo url::base(true)?>purchases/pay/do_payback',
				dataType:'json',
				data:data,
				async:false,
				success:function(re)
				{
					if(re)
					{
						flag = true;
					}
				},
				error:function()
				{
					//alert('error');
				}
			});
		
		

		if(flag)
		{
			var html_string = "添加成功";
			art.dialog({
			    time: 2,
			    content: html_string
			});
			location.reload();
		}
		else
		{
			alert("操作失败");
		}
		}
	});
});
</script>
<?php if(isset($purchase_pays) and $purchase_pays)
{?>
<table  class="table table-bordered">
	<tr>
		<th>采购单号</th>
		<th>采购明细单号</th>
		<!--<th>应付金额</th>  -->
		<th>已付金额</th>
		<th>已退金额</th>
		<th>退款金额</th>
		<th>退款备注</th>
		<th>操作</th>
	</tr>
	<tr>
		<td>
			<span id="po_id"><?php echo $purchase_pays['purchase_pays'][0]['purchaseorder_id']?></span>
		</td>
		<td>
			<span id="pd_id"><?php echo $purchase_pays['purchase_pays'][0]['purchase_detail_id']?></span>
		</td>
		<td>
			<span id="pd_pay_all"><?php echo $purchase_pays['purchase_pays_all'];?></span>
		</td>
		<td>
			<span id="pd_payback_all"><?php echo isset($purchase_paybacks['refund_invoices_all'])?$purchase_paybacks['refund_invoices_all']:0 ?></span>
		</td>
		<td>
			<input type="text" id="payback_refund_money" class="input-mini input_no_space">
		</td>
		<td>
			<input type="text" id="payback_remark" class="input-mini input_no_space">
		</td>
		
		<td>
		<button id="add_payabck_btn" class="btn">确定</button>
		</td>
	</tr>
	
</table>
<?php }?>
<?php  if(isset($purchase_pays) and $purchase_pays
		and isset($purchase_pays['purchase_pays'])
		and $purchase_pays['purchase_pays']){ ?>
<div>付款记录</div>
<table  class="table table-hover table-striped table-condensed"  >
	<tr>
		<th>付款单号</th>
		<th>付款金额</th>
		<th>付款时间</th>
		<th>付款人员</th>
		<th>付款备注</th>
	</tr>
	<?php foreach ($purchase_pays['purchase_pays'] as $purchase_pay) {?>
	<tr>
		<td><?php echo $purchase_pay['id']?></td>
		<td><?php echo $purchase_pay['amount_paid']?></td>
		<td><?php echo $purchase_pay['pay_date']?></td>
		<td><?php $payer = ORM::factory('user',$purchase_pay['payer']);
		if($payer->loaded())
		{
			echo $payer->name;
		}
		 ?></td>
		<td><?php echo $purchase_pay['pay_reason']?></td>
	</tr>
	<?php }?>
</table>
<?php }?>

<?php if($purchase_paybacks and isset($purchase_paybacks['refund_invoices'])){
?>
<div>退款记录</div>
<table  class="table table-hover table-striped table-condensed"  >
	<tr>
		<th>退款单号</th>
		<th>退款金额</th>
		<th>退款时间</th>
		<th>操作人员</th>
		<th>退款原因</th>
	</tr>
	<?php  foreach ($purchase_paybacks['refund_invoices'] as $payback){?>
		<tr>
		<td><?php echo $payback['id']?></td>
		<td><?php echo $payback['refund_money']?></td>
		<td><?php echo $payback['refund_date']?></td>
		<td><?php echo $payback['user_id']?></td>
		<td><?php echo $payback['remark']?></td>
	</tr>
	<?php }?>
</table>
<?php }?>
